package reflect;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 使用反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        ArrayList list = new ArrayList();
        System.out.println(list);

        //1获取要实例化对象的类的类对象

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);

        //2调用Class的newInstance()来调用无参构造器
        Object obj = cls.newInstance();
        System.out.println(obj);
    }
}
